/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;
import java.io.*;

import java.util.Vector;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;
import android.content.Context;
import android.content.res.*;

/* Class Based on .obj Loader for Android project 
 * at http://objloaderforand.sourceforge.net/
 */

public class ObjLoader extends Mesh
{
	/*
private final String TAG = this.getClass().getSimpleName();
	
	private Vector<Float> mVertexList;
	private Vector<Float> mTextureCoordinateList;
	
	private Vector<Short> mVertexBufferList, mTextureBufferList;
	private String[] mAssetLocation, tempLine;
	private String fileData, lineData;
	private BufferedReader inRead;
	private Float[] temp;
	private int count;
	Context mContext;
	


	public ObjLoader(String filename, Context context) 
	{
	 mVertexList = new Vector<Float>();
	 mTextureCoordinateList  = new Vector<Float>();
	 mVertexBufferList  = new Vector<Short>();
	 mTextureBufferList =  new Vector<Short>();
	 
	  	mContext = context;
	  	AssetManager assMang = mContext.getAssets();
		try 
		{
			mAssetLocation = assMang.list("meshs");
		} 
		catch (IOException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int fileIndex = 0;
		while(!filename.equals(mAssetLocation[fileIndex]) )
		{
			Log.i(TAG, " File at Index " + " " + fileIndex  + mAssetLocation[fileIndex]);
			fileIndex++;
		}
		
		
		Float[] tempVert = new Float[3];
		Float[] tempUV = new Float[2];
		Short[] tempTriangle = new Short[3];
		Short[] tempUVlist = new Short[2];
		
		InputStream instream = null;
		try
		{
			instream = assMang.open( "meshs/" +mAssetLocation[fileIndex]) ;
			inRead = new BufferedReader(new InputStreamReader(instream));
			Log.i(TAG, "File opened Successfully");
			while((lineData = inRead.readLine()) != null)
			{
				Log.i(TAG,"LineData" + lineData);
				lineData = lineData.trim();
				tempLine = lineData.split("[ ]");
				
				
			
				
				if(tempLine[0].equalsIgnoreCase("v"))
				{
					for(count = 1; count < tempLine.length; count++)
					{
						mVertexList.add(Float.valueOf(tempLine[count]));
					}
					//Log.i(TAG, "temp vert " + tempVert[0] + " " + tempVert[1]+ " " + tempVert[2] );
					
		
					//Log.i(TAG, "vert data " + mVertexList.get(0)[0] + " " + mVertexList.get(0)[1] + " " + mVertexList.get(0)[2] );
					
					
					
				}
				else if(tempLine[0].equalsIgnoreCase("vt"))
				{
					for(count = 1; count < tempLine.length; count++)
					{
						mTextureCoordinateList.add(Float.valueOf(tempLine[count]));
					}
				}
				//Log.i(TAG,"Cord list size " + mTextureCoordinateList.size());
				
				else if(tempLine[0].equalsIgnoreCase("f"))
				{
					
					Short temp = 0;
					for(count = 1; count < tempLine.length; count++)
					{
						tempLine[count] = tempLine[count].trim();
						String tempIndexStr[] = new String[2];
						tempIndexStr = tempLine[count].split("[/]");
						
					 
						temp = Short.valueOf(tempIndexStr[0] );
						 temp--;
						
						mVertexBufferList.add(temp);
						temp = Short.valueOf(tempIndexStr[1] );
						 temp--;
						 Log.i(TAG, "tempIndexStr[1] " + temp);
						mTextureBufferList.add(Short.valueOf(tempIndexStr[1] ));
						
						
						
										
					}
					
					
					
					
				}
				else
				{
					
				}
				
				
				
				
				
				
				
			}
			
			
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Log.i(TAG, "Texture Index Capacity " + mTextureBufferList.size() );
		
		
		
		
		float tempVertList[] = new float[mVertexList.size()];
		float tempUVList[] = new float[mTextureCoordinateList.size()];
		short tempIndices[] = new short[mVertexBufferList.size()];
		float tempUVbuffer[] = new float[mTextureBufferList.size() * 2];
		//Log.i(TAG, "Vert list Capacity" + tempVertList.length);
		for(count = 0; count  < mVertexList.size(); count++)
		{			
				tempVertList[ count ] = mVertexList.elementAt(count);						
		}
		for (count = 0; count < mTextureCoordinateList.size(); count ++ )
		{
			tempUVList[count] = mTextureCoordinateList.elementAt(count);
		}
		for(count = 0; count < mVertexBufferList.size(); count++)
		{
			tempIndices[count] = (short) (mVertexBufferList.elementAt(count));
			
		}
		short index = 0;
		//Log.i(TAG, "tempUVbuffer.length / 2 = " + (tempUVbuffer.length / 2));
		for(count = 0;count < tempUVbuffer.length / 2 ; count++ )
		{
			
			index =  (short) (mTextureBufferList.elementAt(count ));
			Log.i(TAG, " Index " + index);
			tempUVbuffer[count*2] = tempUVList[index*2]; 
			tempUVbuffer[count*2 +1] = tempUVList[index*2 + 1];
			//Log.i(TAG, "count: " + count + " U: " + tempUVbuffer[count*2] + " V: " + tempUVbuffer[count*2 +1] );
			
		}
	
		 setIndices(tempIndices);
		 setVertices(tempVertList);	
		 setTexCoords(tempUVbuffer);
	
		 for(int i = 0; i < mTextureBuffer.capacity(); i++)
		 {
			 Log.i(TAG, "mTextureBuffer.get(" + i +" )" + mTextureBuffer.get(i));
		 }
					
	}
	
	protected void OnDraw(GL10 gl)
	{
	

		gl.glFrontFace(GL10.GL_CCW);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
		gl.glColor4f(mRGBA[0], mRGBA[1], mRGBA[2], mRGBA[3]);
		
		if(mColorBuffer != null)
		{
			gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
		}
		
		if (mShouldLoadTexture)
		{
			//loadGLTexture(gl);
			mShouldLoadTexture = false;			
		}
		
		if(mTexID != -1 && mTextureBuffer != null)
		{
			//Log.i(TAG, "" + mTextureBuffer.get(0));
			gl.glEnable(GL10.GL_TEXTURE_2D);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTexID);
		}
		
		gl.glTranslatef(mTranslate[0], mTranslate[1], mTranslate[2]);
		gl.glRotatef(mRotation[0], 1, 0, 0 );
		gl.glRotatef(mRotation[1], 0, 1, 0 );
		gl.glRotatef(mRotation[2], 0, 0, 1 );
		
		//gl.glScalef(mScale[0],mScale[1], mScale[2]);
		
		gl.glDrawElements(GL10.GL_TRIANGLES, mNumIndices,
						  GL10.GL_UNSIGNED_SHORT, mIndexBuffer);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		
		
		if(mTexID != -1 && mTextureBuffer != null)
		{
			gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		}
		
		
		
		
		
		
	}
	
  */
	  

}
	
	
	
	
	
	


